// index.js
// pages/index/index.js
const app = getApp()

Page({
  data: {
    showPopup: false, // 控制弹出层的显示和隐藏
    username: '',
    password: ''
  },
  onSavePassword(event){
    this.setData({
      password: event.detail,
    })
  },
  onSaveName(e){
    this.setData({
      username: e.detail,
    })
  },
  toLogin(e){

    wx.request({
      url: app.globalData.url+'/users/checkUserByAccount', // 你的接口地址
      method: 'POST',
      data: {
        username: this.data.username,
        password: this.data.password
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      }, success: (res) => {
        // 请求成功时的回调
        var item = res.data.data;
        app.globalData.username = item.username;
        app.globalData.password = item.password;
        app.globalData.avatar = item.avatar;
        app.globalData.name = item.name;
        app.globalData.gender = item.gender;
        app.globalData.age = item.age;
        app.globalData.bio = item.bio;
        app.globalData.isLogin = true;
        app.globalData.userId = item.uidStr;
        console.log('请求成功', res.data);
        wx.navigateTo({
          url: '/pages/chatlist/chatlist',
          success: function (res) {
            // 跳转成功后的回调
          },
          fail: function (res) {
            // 跳转失败的回调
            console.log(res);
          }
        })
      },
      fail: (err) => {
        // 请求失败时的回调
        console.error('请求失败', err);
      }
    });
  }
});
